package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import edu.sysu.cshare.control.ClassManager;
import edu.sysu.cshare.control.UserManager;
import edu.sysu.cshare.model.Group;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.gui.GUI;
import edu.sysu.cshare.util.gui.TableModel_Course;
import edu.sysu.cshare.util.gui.Table_Course;
import java.awt.Color;

//FIXME 退课成功table中能及时显示 选课不可以
//FIXME 也许 不该用cobombox 来处理组 太蛋疼了
public class Pnl_Setting_Me extends JPanel {
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2 = new JPanel();
	private JLabel lblID;
	private UserManager userManager;
	private ClassManager classManager;
	private User user;
	private JButton btnNewButton_1;
	private JComboBox cmboxQuit;
	private JComboBox cmboxAvailables;
	private Component horizontalStrut_2;
	private JScrollPane scrollPane;
	private JPanel panel_3;
	private Object[] quitings;
	private Object[] availables;

	/**
	 * Create the panel.
	 */

	private void setUpCmbox(){
		if(cmboxAvailables!=null){
			panel_2.remove(cmboxAvailables);
		}if(cmboxQuit!=null){
			panel_2.remove(cmboxQuit);
		}
		availables = new Object[classManager.getAvailableGroups().size() + 1];
		availables[0] = "可加入的组";
		System.arraycopy(classManager.getAvailableGroups().toArray(), 0,
				availables, 1, classManager.getAvailableGroups().size());
		
		quitings = new Object[1 + user.getGroups().size()];
		quitings[0] = "当前已加入的组";
		System.arraycopy(user.getGroups().toArray(), 0, quitings, 1, user
				.getGroups().size());
		cmboxAvailables= new JComboBox(availables);
		cmboxQuit= new JComboBox(quitings);
		
		cmboxAvailables.setToolTipText("选择一个组以加入其中");
		cmboxAvailables.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				try {
					Pnl_Setting_Me.this.userManager.joinGroup((Group) cb
							.getSelectedItem());
					JOptionPane.showMessageDialog(null, "加入成功");

					setUpCmbox();
				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}
		});
		panel_2.setBackground(new Color(245, 245, 220));
		panel_2.add(cmboxAvailables);
		
		
		cmboxQuit = new JComboBox(quitings);
		cmboxQuit.setToolTipText("选择一个组来退出");
		cmboxQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				try {
					Pnl_Setting_Me.this.userManager.quitGroup((Group) cb
							.getSelectedItem());
					JOptionPane.showMessageDialog(null, "退出成功");
					setUpCmbox();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		panel_2.add(cmboxQuit);
		
	}
	private void updateValue() {

		lblID.setText(String.format("ID:%s  姓名:%s  班级:%s", user.getId(),
				user.getName(), user.getClazz().getName()));
		
		
	}

	public Pnl_Setting_Me(UserManager userManager, ClassManager classManager) {
		this.userManager = userManager;
		this.classManager = classManager;
		this.user = userManager.getActor();
		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		panel.setBackground(new Color(245, 245, 220));
		panel.setBorder(new TitledBorder(null, "\u4E2A\u4EBA\u4FE1\u606F",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

		lblID = new JLabel("ID:");
		panel.add(lblID);

		JButton btnNewButton = new JButton("修改");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GUI.showModalDialog(new Dlg_UserDetail(
						Pnl_Setting_Me.this.userManager));
			}
		});
		panel.add(btnNewButton);

		panel_1 = new JPanel();
		panel_1.setBackground(new Color(245, 245, 220));
		panel_1.setBorder(new TitledBorder(null, "\u8BFE\u7A0B\u4FE1\u606F",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		Table_Course table_Course = new Table_Course(new TableModel_Course(
				classManager), classManager, userManager);
		table_Course.setBackground(new Color(245, 245, 220));
		scrollPane = new JScrollPane(table_Course);

		panel_1.add(scrollPane, BorderLayout.CENTER);

		panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.SOUTH);

		panel_2.setBorder(new TitledBorder(null, "\u5C0F\u7EC4\u4FE1\u606F",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(panel_2, BorderLayout.SOUTH);

		Object[] joined = new Object[user.getGroups().size() + 1];
		joined[0] = "当前已经加入的小组";
		System.arraycopy(user.getGroups().toArray(), 0, joined, 1, user
				.getGroups().size());

		
		setUpCmbox();
		
		


		btnNewButton_1 = new JButton("创建");
		panel_2.add(btnNewButton_1);
		updateValue();
	}

}
